package top.igotcha.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import top.igotcha.eduservice.entity.EduCourse;
import top.igotcha.eduservice.entity.EduCourseDescription;
import top.igotcha.eduservice.entity.EduTeacher;
import top.igotcha.eduservice.entity.frontvo.CourseFrontVo;
import top.igotcha.eduservice.entity.frontvo.CourseWebVo;
import top.igotcha.eduservice.entity.vo.CourseInfoVo;
import top.igotcha.eduservice.entity.vo.CourseNewQueryVO;
import top.igotcha.eduservice.entity.vo.CoursePublishVo;
import top.igotcha.eduservice.entity.vo.CourseQueryVO;
import top.igotcha.eduservice.mapper.EduCourseMapper;
import top.igotcha.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.igotcha.commonutils.exception.GuliException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-06
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService courseDescriptionService;
    @Autowired
    EduVideoService videoService;
    @Autowired
    EduChapterService chapterService;
    @Autowired
    EduTeacherService teacherService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0){
            throw new GuliException(20001,"添加课程信息失败");
        }
        String courseId = eduCourse.getId();

        //添加描述信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //描述信息id与课程id相同
        courseDescription.setId(courseId);
        courseDescriptionService.save(courseDescription);
        return courseId;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2.查询描述表
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update==0){
            throw new GuliException(20001,"课程信息修改失败");
        }

        //2.修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublish(String courseId) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    @Override
    public void pageCourseCondition(Page<EduCourse> pageParam, CourseQueryVO vo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        if (vo == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String status = vo.getStatus();
        String title = vo.getTitle();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status) ) {
            queryWrapper.eq("status", status);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void removeCourse(String courseId) {
        //根据课程ID删除小节
        videoService.removeVideoByCourseId(courseId);
        //根据课程ID删除章节
        chapterService.removeChapterByCourseId(courseId);
        //根据课程ID删除描述
        courseDescriptionService.removeById(courseId);

        int result = baseMapper.deleteById(courseId);

        if (result==0){
            throw new GuliException(20001,"删除失败");
        }
    }

    @Override
    public void pageCourseNewCondition(Page<EduCourse> pageParam, CourseNewQueryVO vo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        if (vo == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String teacherName = vo.getTeacherName();
        if (!StringUtils.isEmpty(teacherName)) {
            List<EduTeacher> teacherList = teacherService.getTeacherListByName(teacherName);
            List<String> teacherNameList = new ArrayList<>();
            for (EduTeacher eduTeacher : teacherList) {
                teacherNameList.add(eduTeacher.getId());
            }
            queryWrapper.in("teacher_id",teacherNameList);
        }


        String subjectId = vo.getSubjectId();
        String title = vo.getTitle();
        String subjectParentId = vo.getSubjectParentId();
        String status = vo.getStatus();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(subjectId) ) {
            queryWrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId) ) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(status) ) {
            queryWrapper.eq("status", status);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }


    //1 条件查询带分页查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

}
